package top.huhuiyu.springboot3.p2301.service.api.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import top.huhuiyu.springboot3.p2301.base.R;
import top.huhuiyu.springboot3.p2301.config.ApiConfigInfo;
import top.huhuiyu.springboot3.p2301.entity.api.*;
import top.huhuiyu.springboot3.p2301.service.RedisService;
import top.huhuiyu.springboot3.p2301.service.api.AMapService;
import top.huhuiyu.springboot3.p2301.util.IpUtils;
import top.huhuiyu.springboot3.p2301.util.JsonUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class AMapServiceImpl implements AMapService {
    private final RedisService redisService;
    private final ApiConfigInfo apiConfigInfo;
    private final RestTemplate restTemplate;

    @Override
    public String queryWeather(String city, String extensions) {
        String url = String.format(apiConfigInfo.getAmapUrl().getWeather(), apiConfigInfo.getAmap_weather_key(), city, extensions);
        log.debug("查询天气的url:{}", url);
        String result = restTemplate.exchange(url, HttpMethod.GET, null, String.class).getBody();
        log.debug("查询天气的结果:{}", result);
        return result;
    }

    @Override
    public R<WeatherLives> queryWeatherBase(String city) {
        String key = String.format(WEATHER_BASE, city);
        // 查看缓存中是否存在天气信息
        if (redisService.has(key)) {
            return R.ok(redisService.load(key, WeatherLives.class));
        }
        // 不存在缓存就去高德api中获取
        String result = queryWeather(city, "base");
        Weather weather = JsonUtils.parse(result, Weather.class);
        if (weather == null || weather.getLives() == null || weather.getLives().size() == 0) {
            return R.fail("无法获取天气信息", null);
        }
        WeatherLives lives = weather.getLives().get(0);
        // 缓存到redis中
        redisService.save(key, lives, LIVE_CACHE_TIME);
        return R.ok(lives);
    }

    @Override
    public R<List<WeatherCasts>> queryWeatherAll(String city) {
        String key = String.format(WEATHER_ALL, city);
        // 查看缓存中是否存在天气信息
        if (redisService.has(key)) {
            return R.ok(redisService.load(key, List.class));
        }
        // 不存在缓存就去高德api中获取
        String result = queryWeather(city, "all");
        Weather weather = JsonUtils.parse(result, Weather.class);
        if (weather == null || weather.getForecasts() == null || weather.getForecasts().size() == 0) {
            return R.fail("无法获取天气信息", null);
        }
        List<WeatherCasts> forecasts = weather.getForecasts().get(0).getCasts();
        // 缓存到redis中
        redisService.save(key, forecasts, CAST_CACHE_TIME);
        return R.ok(forecasts);
    }

    @Override
    public R<IpResult> queryIpInfo(String ip) {
        // 没有ip地址，则获取客户端的ip地址
        if (!StringUtils.hasText(ip)) {
            ip = IpUtils.getIpAddress();
        }
        // 如果是本地内网ip就直接失败
        if (IpUtils.intranet(ip)) {
            return R.fail("无法获取内网ip地址信息", null);
        }
        // 获取ip相关的地址信息
        String url = String.format(apiConfigInfo.getAmapUrl().getIp(), apiConfigInfo.getAmap_weather_key(), ip);
        String result = restTemplate.exchange(url, HttpMethod.GET, null, String.class).getBody();
        return R.ok(JsonUtils.parse(result, IpResult.class));
    }

    @Override
    public R<GeoResult> queryGeo(String address, String city) {
        String url = String.format(apiConfigInfo.getAmapUrl().getGeo(), apiConfigInfo.getAmap_weather_key(), address, city);
        String result = restTemplate.exchange(url, HttpMethod.GET, null, String.class).getBody();
        log.debug("地理编码结果：{}", result);
        // 111.682335,29.058729
        return R.ok(JsonUtils.parse(result, GeoResult.class));
    }

}
